var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), a = e(require("../../@babel/runtime/regenerator")), r = require("../../@babel/runtime/helpers/asyncToGenerator"), s = e(require("../../http/address")), d = e(require("../../http/order")), n = require("../../utils/tool"), i = new s.default(), o = new d.default();

Page({
    data: {
        show: !1,
        addressDetails: {},
        paramData: {
            omsOrderList: []
        },
        orderIds: "",
        orderList: [],
        postInformation: {},
        addPickParams: {
            orderIds: "",
            addressId: "",
            submitType: "",
            couponHistoryId: "",
            shippingPriceTemplateId: ""
        },
        needPost: !1
    },
    onLoad: function(e) {
        this.setParamData(e), this.getDefaultAddress();
    },
    test: function() {
        Promise.all([ i.getDefaultAddress(), o.getMergeOrderList(this.data.addPickParams) ]).then(function(e) {
            console.log(e);
        }).catch(function(e) {
            console.log(e);
        }), Promise.all([ o.getMergeOrderList(this.data.addPickParams), i.getDefaultAddress() ].map(function(e) {
            return e.catch(function(e) {
                console.log(e, "error");
            });
        })).then(function(e) {
            console.log(e, "res");
        }).catch(function(e) {
            console.log(e, "error");
        });
    },
    onShow: function() {
        this.getList();
    },
    showPostage: (0, n.throttle)(r(a.default.mark(function e() {
        var t, r;
        return a.default.wrap(function(e) {
            for (;;) switch (e.prev = e.next) {
              case 0:
                if (e.prev = 0, this.data.addressDetails.addressId) {
                    e.next = 4;
                    break;
                }
                return (0, n.toastToPage)("请先新增一个收货地址再,进行申请发货"), e.abrupt("return");

              case 4:
                return wx.showLoading({
                    mask: !0,
                    title: "加载中…"
                }), t = this.data.orderIds, e.next = 8, o.postage({
                    orderIds: t
                });

              case 8:
                r = e.sent, this.setData({
                    needPost: r.data.openStatus
                }), 1 == r.data.openStatus ? (this.getPostInformation(), this.setData({
                    show: !0
                })) : (this.setData({
                    addPickParams: {
                        addressId: this.data.addressDetails.addressId,
                        orderIds: this.data.orderIds,
                        submitType: 0,
                        shippingPriceTemplateId: "",
                        couponHistoryId: ""
                    }
                }), this.addPick()), e.next = 16;
                break;

              case 13:
                e.prev = 13, e.t0 = e.catch(0), wx.hideLoading();

              case 16:
              case "end":
                return e.stop();
            }
        }, e, this, [ [ 0, 13 ] ]);
    })), 3e3),
    addPick: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r, s;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return wx.showLoading({
                        title: "加载中…",
                        mask: !0
                    }), t.prev = 1, t.next = 4, o.addPick(e.data.addPickParams);

                  case 4:
                    r = t.sent, e.data.needPost && 2 == e.data.addPickParams.submitType ? (s = r.data.mergeOrderId, 
                    e.startPay(s)) : (wx.hideLoading(), e._sendRefresh(), (0, n.toastToPage)("申请发货成功", "/pages/pick_records/pick_records")), 
                    t.next = 11;
                    break;

                  case 8:
                    t.prev = 8, t.t0 = t.catch(1), wx.hideLoading();

                  case 11:
                    return t.prev = 11, t.finish(11);

                  case 13:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 1, 8, 11, 13 ] ]);
        }))();
    },
    getPostInformation: function() {
        var e = this;
        return r(a.default.mark(function r() {
            var s, d, n, i, c, u;
            return a.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    return s = {
                        addressId: e.data.addressDetails.addressId,
                        omsOrderList: e.data.orderIds.split(",")
                    }, a.next = 3, o.postInformation(s);

                  case 3:
                    d = a.sent, n = [], i = t(d.data.omsOrderShippingPriceVOList);
                    try {
                        for (i.s(); !(c = i.n()).done; ) u = c.value, n.push({
                            id: u.shippingTemplatePriceId,
                            money: u.shippingMoney,
                            name: u.expressCompanyName,
                            submitType: 2
                        });
                    } catch (e) {
                        i.e(e);
                    } finally {
                        i.f();
                    }
                    d.data.amsCouponHistoryVO && n.push({
                        id: d.data.amsCouponHistoryVO.couponHistoryId,
                        money: "",
                        name: "免邮券",
                        submitType: 1
                    }), e.setData({
                        postInformation: n,
                        addPickParams: {
                            addressId: e.data.addressDetails.addressId,
                            orderIds: e.data.orderIds,
                            couponHistoryId: "",
                            submitType: n[0].submitType,
                            shippingPriceTemplateId: n[0].id
                        },
                        freeData: {
                            freeNum: d.data.freeNum,
                            freePrice: d.data.freePrice
                        }
                    }), wx.hideLoading();

                  case 10:
                  case "end":
                    return a.stop();
                }
            }, r);
        }))();
    },
    onClickHide: function() {
        this.setData({
            show: !1
        });
    },
    getDefaultAddress: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, i.getDefaultAddress();

                  case 2:
                    r = t.sent, e.setData({
                        addressDetails: r.data
                    });

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    getAddressDetail: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r, s, d, n;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (r = getCurrentPages(), s = r[r.length - 1], !(d = s.data.addressId)) {
                        t.next = 8;
                        break;
                    }
                    return t.next = 6, i.addressDetails(d);

                  case 6:
                    n = t.sent, e.setData({
                        addressDetails: n.data
                    });

                  case 8:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    getList: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, o.getMergeOrderList(e.data.paramData);

                  case 2:
                    r = t.sent, e.setData({
                        orderList: r.data
                    }), e.getAddressDetail();

                  case 5:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    setParamData: function(e) {
        var t = e.orderId.split(",");
        this.setData({
            paramData: {
                omsOrderList: t
            },
            orderIds: e.orderId
        });
    },
    setPost: function(e) {
        2 == e.detail.submitType ? this.setData({
            addPickParams: {
                addressId: this.data.addressDetails.addressId,
                orderIds: this.data.orderIds,
                submitType: e.detail.submitType,
                couponHistoryId: "",
                shippingPriceTemplateId: e.detail.id
            }
        }) : this.setData({
            addPickParams: {
                addressId: this.data.addressDetails.addressId,
                orderIds: this.data.orderIds,
                submitType: e.detail.submitType,
                couponHistoryId: e.detail.id,
                shippingPriceTemplateId: ""
            }
        });
    },
    startPay: function(e) {
        var t = this;
        return r(a.default.mark(function r() {
            var s, d, i, c, u;
            return a.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    return a.next = 2, (0, n.promising)(wx.login)();

                  case 2:
                    return s = a.sent, d = s.code, a.next = 6, o.getPostWxPayInfo(d, e);

                  case 6:
                    i = a.sent, c = i.data, u = {
                        timeStamp: c.timeStamp,
                        nonceStr: c.nonceStr,
                        package: c.package,
                        paySign: c.paySign,
                        signType: c.signType,
                        success: function(e) {
                            console.log(e), wx.hideLoading(), t._sendRefresh(), (0, n.toastToPage)("申请发货成功", "/pages/pick_records/pick_records");
                        },
                        fail: function(t) {
                            console.log(t), o.cancelPick(e), wx.hideLoading();
                        }
                    }, wx.requestPayment(u);

                  case 10:
                  case "end":
                    return a.stop();
                }
            }, r);
        }))();
    },
    toAddress: function() {
        wx.navigateTo({
            url: "/pages/address/address"
        });
    },
    _toPickRecord: function() {
        (0, n.toastToPage)("申请发货成功"), setTimeout(function() {
            wx.reLaunch({
                url: "/pages/pick_records/pick_records"
            });
        });
    },
    _sendRefresh: function(e) {
        var t = getCurrentPages();
        console.log(t), t[t.length - 2].setData({
            isRefresh: !0
        });
    },
    onReady: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {}
});